package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.OrganAddress;
import com.itheima.service.OrganAddressService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.UUID;

/**
 * 机构地址管理
 */
@RestController
@RequestMapping("/organAddress")
public class OrganAddressController {

    @Reference
    private OrganAddressService organAddressService;
    @Autowired
    private JedisPool jedisPool;

    //新增
    @RequestMapping("/add")
    public Result add(@RequestBody OrganAddress organAddress){
        try {
            organAddressService.add(organAddress);
        }catch (Exception e){
            //新增失败
            return new Result(false, MessageConstant.ADD_ORGAN_ADDRESS_FAIL);
        }
        //新增成功
        return new Result(true, MessageConstant.ADD_ORGAN_ADDRESS_SUCCESS);
    }

    //分页查询upload
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = organAddressService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }

    //根据id查询
    @RequestMapping("/findById")
    public Result findById(Integer id){
        OrganAddress organAddress = organAddressService.findById(id);
        if(organAddress != null){
            Result result = new Result(true, MessageConstant.QUERY_ORGAN_ADDRESS_SUCCESS);
            result.setData(organAddress);
            return result;
        }
        return new Result(false,MessageConstant.QUERY_ORGAN_ADDRESS_FAIL);
    }

    //根据检查组合id查询对应的所有检查项id
//    @RequestMapping("/findCheckItemIdsByCheckGroupId")
//    public Result findCheckItemIdsByCheckGroupId(Integer id){
//        try{
//            List<Integer> checkitemIds = organAddressService.findCheckItemIdsByCheckGroupId(id);
//            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkitemIds);
//        }catch (Exception e){
//            e.printStackTrace();
//            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
//        }
//    }

    //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody OrganAddress organAddress){
        try {
            organAddressService.edit(organAddress);
        }catch (Exception e){
            return new Result(false, MessageConstant.EDIT_ORGAN_ADDRESS_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_ORGAN_ADDRESS_SUCCESS);
    }

    //查询所有
    @RequestMapping("/findAll")
    public Result findAll(){
        List<OrganAddress> organAddressList = organAddressService.findAll();
        if(organAddressList != null && organAddressList.size() > 0){
            Result result = new Result(true, MessageConstant.QUERY_ORGAN_ADDRESS_SUCCESS);
            result.setData(organAddressList);
            return result;
        }
        return new Result(false, MessageConstant.QUERY_ORGAN_ADDRESS_FAIL);
    }

    //图片上传
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile){
        try{
            //获取原始文件名
            String originalFilename = imgFile.getOriginalFilename();
            int lastIndexOf = originalFilename.lastIndexOf(".");
            //获取文件后缀
            String suffix = originalFilename.substring(lastIndexOf - 1);
            //使用UUID随机产生文件名称，防止同名文件覆盖
            String fileName = UUID.randomUUID().toString() + suffix;

            QiniuUtils.upload2Qiniu(imgFile.getBytes(), fileName);

            //图片上传成功
            Result result = new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS);
            result.setData(fileName);

            //将上传图片名称存入Redis，基于Redis的Set集合存储
            jedisPool.getResource().sadd(RedisConstant.ORGAN_ADDRESS_PIC_DB_RESOURCES, fileName);

            return result;
        }catch (Exception e){
            e.printStackTrace();
            //图片上传失败
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    @RequestMapping("/deleteById")
    public Result delete(String id) {
        try {
            organAddressService.deleteById(id);
        }catch (RuntimeException e){
            return new Result(false,e.getMessage());
        }catch (Exception e){
            return new Result(false, MessageConstant.DELETE_ORGAN_ADDRESS_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_ORGAN_ADDRESS_SUCCESS);
    }

    //编辑
    @RequestMapping("/findOrgans")
    public Result findOrgans(String lng, String lat){
        try {
            List<OrganAddress> addresses = organAddressService.findOrgans(lng, lat);
            return new Result(true, MessageConstant.QUERY_ORGAN_ADDRESS_SUCCESS, addresses);
        }catch (Exception e){
            return new Result(false, MessageConstant.QUERY_ORGAN_ADDRESS_FAIL);
        }
    }
}
